package com.blue.code.ftl;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.blue.code.bean.EntityBean;

import freemarker.template.Configuration;
import freemarker.template.Template;

@Component
public class ExceptionHandler implements Handler
{
	private static Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
	
	@Value("${ftl.exception}")
	private String exception;
	
	private Template template;
	
	public ExceptionHandler()
	{
	}

	@Override
	public void handle(EntityBean bean, String output, String pkg) throws Exception
	{
		Map<String, Object> root = new HashMap<>();
		root.put("pkg", pkg + "." + exception);
		root.put("clazz", bean.getClazz());
		root.put("name", bean.getName());
		root.put("table", bean.getTable());
		
		String clazz = bean.getClazz() + "Exception";
		String fileName = this.output(template, root, output, exception, clazz);
		logger.info("生成文件：{}", fileName);
	}

	@Override
	public void initConfiguration(Configuration conf, String ext) throws Exception
	{
		String ftl = exception + ext;
		template = conf.getTemplate(ftl);
		logger.info("初始化 Exception 模板：{}", ftl);
	}

}
